import 'dart:convert';

import 'package:dio/dio.dart';



const String BASE_URL = "http://139.199.108.119:9876";

Future<void> report(String name, int num1, int num2, int num3) async {
  if(name.isEmpty) {
    print("name 空");
    return;
  }
  Response response = await Dio().post(BASE_URL + "/report",
      data: {"name": name, "num": [num1, num2, num3]});
  if(response.statusCode == 200) {
    print(response.data);
  }
}

Future<List<Score>> getScore() async {
  Response response = await Dio().get(BASE_URL + "/score");
  print(response.statusCode);
  print(response.data);
  if(response.statusCode == 200) {
    return Score.fromMaps(json.decode(response.data));
  }
  return <Score>[];
}

class Score {
  final String name;
  final List<String> scores;

  Score(this.name, this.scores);

  static Score fromMap(Map<String, dynamic> map) {
    return Score(map["name"], map["num"].toString().split(","));
  }


  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is Score &&
              runtimeType == other.runtimeType &&
              name == other.name;

  @override
  int get hashCode => name.hashCode;

  static List<Score> fromMaps(List<dynamic> map) {

    return map.map((f) => fromMap(f)).toList();
  }

  @override
  String toString() {
    return 'Score{name: $name, scores: $scores}';
  }


}